﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using CustomsSystem.BLL;
using CustomsSystem.Model;

namespace WEB.Client
{
    public partial class ClientSheetView1 : System.Web.UI.Page
    {
        public DataSet ds;
        protected void Page_Load(object sender, EventArgs e)
        {            
            if (!Page.IsPostBack)
            {
                SheetInfoManage sheetManage = new SheetInfoManage();
                ds = sheetManage.GetView("CustomerName = '" + Session["ClientName"] +"'");
                ViewState["data"] = (DataTable)ds.Tables[0];
                gvSheetInfo1.DataSource = ds;
                gvSheetInfo1.DataBind();
            }
        }

        protected void gvSheetInfo1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            int id = Convert.ToInt32(gvSheetInfo1.DataKeys[e.NewEditIndex].Value);
            Response.Redirect("~/score/scoreEdit.aspx?ID=" + id);
            Response.Redirect("~/score/handleView.aspx?ID=" + id);
            Response.Redirect("~/Client/sheetDetails.aspx?ID=" + id);
        }

        protected void btnQuery1_Click(object sender, EventArgs e)
        {
            string strQuery1 = ddlQuery1.SelectedValue;
            SheetInfoManage sheetManage = new SheetInfoManage();
            string strSql = "";
            if (tbQuery1.Text != "")
            {
                switch (ddlQuery1.SelectedIndex)  //根据选项传不同的SQL语句
                {
                    case 1:
                        strSql = string.Format("{0}1 = '{1}' or {0}2 = '{1}' or {0}3 = '{1}'", strQuery1, tbQuery1.Text);
                        break;
                    case 0:
                    case 2:
                    case 3:
                    case 5:
                        strSql = string.Format("{0} = '{1}'", strQuery1, tbQuery1.Text);
                        break;
                    case 4:
                        strSql = string.Format("{0} like '%{1}%'", strQuery1, tbQuery1.Text);
                        break;
                }
                strSql += "and CustomerName = '" + Session["ClientName"] + "'";
            }
            else
            {
                strSql = "CustomerName = '" + Session["ClientName"] + "'";
                Response.Write("<script>alert('请输入查找信息')</script>");
            }
            DataSet ds = new DataSet();
            ds = sheetManage.GetView(strSql);
            ViewState["data"] = (DataTable)ds.Tables[0];
            gvSheetInfo1.DataSource = ds;
            gvSheetInfo1.DataBind();
        }

        /// <summary>
        /// 网页跳转
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvSheetInfo1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string index = e.CommandArgument.ToString(); //那行被选中,取出选中行的索引 
            if (e.CommandName == "sheetID")
            {
                int id = Convert.ToInt32(gvSheetInfo1.DataKeys[int.Parse(index)].Value);
                Response.Redirect("~/Client/sheetDetails1.aspx?ID=" + id);
            }
            if (e.CommandName == "Score")
            {
                int id = Convert.ToInt32(gvSheetInfo1.DataKeys[int.Parse(index)].Value);
                Response.Redirect("~/score/scoreEdit.aspx?ID=" + id);
            }
            if (e.CommandName == "Handle")
            {
                int id = Convert.ToInt32(gvSheetInfo1.DataKeys[int.Parse(index)].Value);
                Response.Redirect("~/score/handleView.aspx?ID=" + id);
            }
        }

        protected void gvSheetInfo1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvSheetInfo1.PageIndex = e.NewPageIndex;
            gvSheetInfo1.DataSource = ViewState["data"];
            gvSheetInfo1.DataBind();//重新绑定GridView数据的函数
        }

        protected void gvSheetInfo1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                WorkflowHandleManage bll = new WorkflowHandleManage();
                //CustomsSystem.Model.workflow model;
                int sheetID = Convert.ToInt32(e.Row.Cells[0].Text);
                //判断报关还是报检
                CustomsSystem.Model.workflow model;
                model = bll.GetModel(sheetID);
                WorkflowManage bllSet = new WorkflowManage();
                int type = 0;
                if (model.infoWKID != null) type = model.infoWKID.Value;
                Info_workflowMaster modelMaster = bllSet.GetModelMaster(type);
                if (modelMaster != null)
                {
                    if (modelMaster.sheetTypeID == 1) type = 1;
                    else if (modelMaster.sheetTypeID == 2) type = 2;
                }
                int IntimeFlag = bll.GetIntime(sheetID,type);//判断及时度

                Image tmp_ImageIntime = (Image)e.Row.Cells[6].FindControl("ImgIntime");//获得图片列的控件
                if (tmp_ImageIntime != null)
                {
                    string imagepath = "";
                    if (IntimeFlag == 1)
                    {
                        imagepath = "../images/status/Green.png";
                    }
                    else if (IntimeFlag == 2)
                    {
                        imagepath = "../images/status/Yellow.png";
                    }
                    else
                    {
                        imagepath = "../images/status/Red.png";
                    }
                    tmp_ImageIntime.ImageUrl = imagepath;//显示图片
                }
            }
        }
    }
}